package com.op.teachingliteapp.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.op.teachingliteapp.bean.FileBean;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class DirUtils {

	private static final String TAG = DirUtils.class.getSimpleName();

	private Context mContext;
	private String baseDir;
	private String userDir;
	private String allCourseDir;

	public static final String DIR_PATH_SEPARATOR = "/";
	private static final String[] COURSES_RESOURCE_DETAILS = { "课程ppt", "上机实验", "视频资料", "课程案例", "课程计划", "学生名单" };

	public DirUtils(Context context, String userId) {
		this.mContext = context;
		this.baseDir = mContext.getFilesDir().getAbsolutePath();
		this.userDir = baseDir + "/user-" + userId + DIR_PATH_SEPARATOR;
		this.allCourseDir = userDir + "/Course" + DIR_PATH_SEPARATOR;
	}

	public void initDirs(List<String> courses) {
		if (ListUtils.isEmpty(courses)) {
			return;
		}

		FileUtils.makeDirs(userDir);
		FileUtils.makeDirs(allCourseDir);

		for (String course : courses) {
			String courseDir = allCourseDir + course + DIR_PATH_SEPARATOR;
			FileUtils.makeDirs(courseDir);
//			Log.i(TAG, "create dir : " + courseDir);
			for (int i = 0; i < COURSES_RESOURCE_DETAILS.length; i++) {
				String detailDir = courseDir + COURSES_RESOURCE_DETAILS[i] + DIR_PATH_SEPARATOR;
				FileUtils.makeDirs(detailDir);
//				Log.i(TAG, "create dir : " + detailDir);
			}
		}
	}
	
	public void createDirs(List<String> courses, String path) {
		if (ListUtils.isEmpty(courses)) {
			return;
		}

		FileUtils.makeDirs(userDir);
		FileUtils.makeDirs(allCourseDir);

		for (String course : courses) {
			String courseDir = path + DIR_PATH_SEPARATOR + course + DIR_PATH_SEPARATOR;
			FileUtils.makeDirs(courseDir);
//			Log.i(TAG, "create dir : " + courseDir);
			for (int i = 0; i < COURSES_RESOURCE_DETAILS.length; i++) {
				String detailDir = courseDir + COURSES_RESOURCE_DETAILS[i] + DIR_PATH_SEPARATOR;
				FileUtils.makeDirs(detailDir);
//				Log.i(TAG, "create dir : " + detailDir);
			}
		}
	}

	public void initFiles() {
		String copyFileDir = Environment.getExternalStorageDirectory().getAbsolutePath();
		Log.i(TAG, copyFileDir);
	}

	/**
	 * 获取文件列表信息，传入相对路径，例："C语言/ppt"
	 * 
	 * @param currentDir
	 * @return
	 */
	public List<FileBean> getFilesNameByDir(String currentDir) {
		String path = userDir + currentDir + DIR_PATH_SEPARATOR;
		return getFileList(path);
	}

	/**
	 * 获取文件列表信息，传入绝对路径，例：/data/data/[package]/files/[user-id]/C语言/ppt
	 * 
	 * @param path
	 * @return
	 */
	public List<FileBean> getFileList(String path) {
		if (!FileUtils.isFolderExist(path)) {
			return null;
		}

		List<FileBean> list = new ArrayList<FileBean>();

		File[] files = new File(path).listFiles();
//		Log.i(TAG, path);
		for (File file : files) {
			Log.i(TAG, file.getAbsolutePath());
			FileBean fileBean = new FileBean();
			fileBean.setName(file.getName());
			fileBean.setPath(file.getAbsolutePath());
			fileBean.setUpdated(TimeUtils.getTime(file.lastModified()));
			fileBean.setCondition("");
			if (file.isFile()) {
				fileBean.setSize(FileSizeUtils.FormetFileSize(file.length())); // 是文件才获取大小，文件夹暂时先不获取
				fileBean.setFile(true);
			} else {
				fileBean.setFile(false);
			}
			list.add(fileBean);
		}
		
		return list;
	}
	
}
